package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SysCategory;
import com.ruoyi.system.mapper.SysCategoryMapper;
import com.ruoyi.system.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class SysCategoryServiceImpl implements SysCategoryService {
    @Autowired
    SysCategoryMapper categoryMapper;
    @Override
    public List<Map> queryAll() {
        return categoryMapper.queryAll(0);
    }

    @Override
    public List<SysCategory> querylist() {
        return categoryMapper.querylist();
    }

    @Override
    public int insert(SysCategory category) {
        //父类别下有没有子类别
        List list=categoryMapper.queryChildren(category.getParentId());
        if(list.size()>0){
            //有子类别
            //查询最大编码+1
            String maxcode=categoryMapper.queryMaxCodeById(category.getParentId());
            //有最大编码
            if(null!=maxcode){
                String code=Integer.parseInt(maxcode)+1+"";
                category.setCode(code);
            }else{
                //没有最大编码
                SysCategory s1=categoryMapper.queryById(category.getParentId());
                String code=s1.getCode()+"01";
                category.setCode(code);
            }
        }else{
            //父类别下没有子类别
            //将父类别最大编码+01
            SysCategory s1=categoryMapper.queryById(category.getParentId());
            category.setCode(s1.getCode()+"01");
        }
        return categoryMapper.insert(category);
    }
}
